package com.example.wechatlistview.controls;

import android.view.View;

import androidx.annotation.LayoutRes;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wechatlistview.resmanager.itemType;

import java.util.List;
import java.util.Map;

import io.github.luizgrp.sectionedrecyclerviewadapter.Section;
import io.github.luizgrp.sectionedrecyclerviewadapter.SectionParameters;
import io.github.luizgrp.sectionedrecyclerviewadapter.utils.EmptyViewHolder;

/**
 * @author CVLocke 陈炜龙
 * 列表分组类
 */
public class ListSection extends Section {

    private OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;

    /**
     * section成员
     */
    List<Map<String, Object>> mList;
    String[] mKey;
    itemType.holderType mType;


    public ListSection(List<Map<String, Object>> list, itemType.holderType type, String[] key, @LayoutRes int itemId, @LayoutRes int headId) {
        super(SectionParameters.builder()
                .itemResourceId(itemId)
                .headerResourceId(headId)
                .build());
        mList = list;
        mType = type;
        mKey = key;
    }


    @Override
    public RecyclerView.ViewHolder getHeaderViewHolder(View view) {

        return new EmptyViewHolder(view);

    }

    @Override
    public int getContentItemsTotal() {
        return mList.size();
    }

    @Override
    public RecyclerView.ViewHolder getItemViewHolder(View view) {
        switch (mType) {
            case discover:
                return new discoverHolder(view);
            default:
                return new EmptyViewHolder(view);
        }

    }

    /**
     * 数据绑定
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (mType) {
            case discover:
                ((discoverHolder) holder).bind(mKey, mList, position);
                break;
            default:
                break;
        }
        if (onRecyclerViewItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getAdapterPosition();
                    onRecyclerViewItemClickListener.onItemClick(holder.itemView, pos);
                }
            });
        }


    }

    /**
     * 事件监听
     */
    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }


    public interface OnRecyclerViewItemClickListener {
        /**
         * @param v        控件
         * @param position 位置
         *                 列表组件点击事件
         */
        void onItemClick(View v, int position);
    }


}
